<?php
// function __autoload($class_name) {
//         include 'class.' . strtolower($class_name) . '.php';
//     }
require_once('class.database.php');

class TundaDao {    

    public function cekTunda($nip,$tahun){
        $db=Database::getInstance();
        $con=$db->getConnection(1);

        $query="SELECT * FROM t_tunda WHERE nip='$nip' AND tahun='$tahun'";

        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount()==1){
            return true;
        }

        return false;
    }
    public function getTundaByNip($nip,$tahun){
        $db=Database::getInstance();
        $con=$db->getConnection(1);

        $query="SELECT * FROM t_tunda WHERE nip='$nip' AND tahun='$tahun'";

        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount()==1){
            return $result->fetch();
        }

        return false;
    }

    public function getTunda($id){
        $db=Database::getInstance();
        $con=$db->getConnection(1);

        $query="SELECT * FROM t_tunda WHERE id='$id'";
        //print_r($query);
        $result=$con->prepare($query);
        $result->execute();
 
        if($result->rowCount()==1){
            $data=$result->fetch();
            return $data;
        }

        return false;
    }

    public function getAllTunda($tahun){
        $db=Database::getInstance();
        $con=$db->getConnection(1);
        $query="SELECT * FROM t_tunda WHERE tahun='$tahun'";
        
        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount()>=1){
            return $result->fetchAll();
        }

        return false;
    }


    public function createTunda(Tunda $tunda){
        $db= Database::getInstance();
        $con=$db->getConnection(1);


        $query="INSERT INTO t_tunda(nip,jumlah,tahun) ";
        $query.=" VALUES('$tunda->nip','$tunda->jumlah','$tunda->tahun')";

        //print_r($query);

        //$data=array($user->nip,$password,$user->id_parent_user,$user->id_kewenangan,$date_created,$user->user_creating);

        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount()==1){
            return true;
        }

        return false;
    }

    public function deleteTunda($id){
        $db=Database::getInstance();
        $con=$db->getConnection(1);

        $query="DELETE FROM t_tunda WHERE id='$id'";

        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount()==1){
            return true;
        }

        return false;
    }

    public function updateTunda(Tunda $tunda){
        $db=Database::getInstance();
        $con=$db->getConnection(1);

        $query="UPDATE t_tunda SET nip='$tunda->nip', jumlah='$tunda->jumlah',tahun='$tunda->tahun' 
        	 WHERE id='$tunda->id'";

        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount()==1){
            return true;
        }

        return false;
    }

}


class Tunda {
	var $id;
	var $nip;
	var $jumlah;
	var $tahun;
}
?>